window.addEventListener("DOMContentLoaded", () => {
    document.querySelectorAll(".choice-parameter-data-holder").forEach((dataHolder) => {
        const {paramName, filterLength} = dataHolder.dataset;
        UnoChoice.renderChoiceParameter(paramName, filterLength);
    });
});

async function executeSql(data) {
    const response_crumb = await fetch('/crumbIssuer/api/json');
    const crumb_data = await response_crumb.json();
    const response = await fetch('/database/executeSql', {
        headers: {
            'Content-Type': 'application/json',
            [crumb_data.crumbRequestField]: crumb_data.crumb// 添加 crumb 到请求头
        },
        method: 'POST',
        body: JSON.stringify(data)
    });
    return response.json();
}

async function jobName() {
    const response_crumb = await fetch('/crumbIssuer/api/json');
    const crumb_data = await response_crumb.json();
    const response = await fetch('/database/jobName', {
        headers: {
            'Content-Type': 'application/json',
            [crumb_data.crumbRequestField]: crumb_data.crumb// 添加 crumb 到请求头
        },
        method: 'POST'
    });
    return response.json();
}

async function getFillBranchItems(data) {
    const response_crumb = await fetch('/crumbIssuer/api/json');
    const crumb_data = await response_crumb.json();
    const response = await fetch('/database/fillBranchItems', {
        headers: {
            'Content-Type': 'application/json',
            [crumb_data.crumbRequestField]: crumb_data.crumb// 添加 crumb 到请求头
        },
        method: 'POST',
        body: JSON.stringify(data)
    });
    return response.json();
}

async function getNodes(data) {
    const response_crumb = await fetch('/crumbIssuer/api/json');
    const crumb_data = await response_crumb.json();
    const response = await fetch('/database/nodes', {
        headers: {
            'Content-Type': 'application/json',
            [crumb_data.crumbRequestField]: crumb_data.crumb// 添加 crumb 到请求头
        },
        method: 'POST',
        body: JSON.stringify(data)
    });
    return response.json();
}

async function getLabels(data) {
    const response_crumb = await fetch('/crumbIssuer/api/json');
    const crumb_data = await response_crumb.json();
    const response = await fetch('/labels', {
        headers: {
            'Content-Type': 'application/json',
            [crumb_data.crumbRequestField]: crumb_data.crumb// 添加 crumb 到请求头
        },
        method: 'POST',
        body: JSON.stringify(data)
    });
    return response.json();
}

function getVal(parent, name) {
    var el = parent.querySelectorAll("[name=" + name + "]")
    if (el.length > 0) {
        return el[0].value;
    }
    return "";
}

async function getAllFillBranchItems(randomName) {
    console.error("randomName:" + randomName)
    var element = document.getElementById(randomName);
    var eles = element.getElementsByClassName("repository_info");
    console.log(eles)
    for (let i = 0; i < eles.length; i++) {
        var repository_info = eles[i];
        var url = getVal(repository_info, "url")
        var credentialsId = getVal(repository_info, "credentialsId")
        var branch = getVal(repository_info, "branch")
        var repositoryName = getVal(repository_info, "repositoryName")
        getBranchItems(repository_info, url, credentialsId, branch, repositoryName).then(() => {
            changeVal(randomName)
        })
    }
}


async function getBranchItems(repository_info,url,credentialsId,branch,repositoryName) {
    const data = await getFillBranchItems({url: url, defaultBranch: branch, credentialsId: credentialsId});
    console.error("repositoryName:" + repositoryName);
    var selectElement = document.getElementById("branch_" + repositoryName);
    console.log(selectElement);
    for (var option of data.values) {
        var opt = document.createElement("option");
        opt.value = option.value;
        opt.text = option.name;
        opt.selected = (opt.value == branch);
        selectElement.add(opt);
    }
}


function changeVal(randomName) {
    var val = []
    var element = document.getElementById(randomName);
    var eles = element.getElementsByClassName("repository_info");
    for (let i = 0; i < eles.length; i++) {
        var repository_info = eles[i];
        var item = {}
        const repositorys = repository_info.querySelectorAll(".repository_val");
        for (const repository of repositorys) {
            var name = repository.getAttribute("name")
            if (name == null || name == NaN) continue;
            item[name] = repository.value;
        }
        val.push(item);
        console.log(item);
    }
    document.getElementById(randomName + "_val").value = JSON.stringify(val);
    console.log(val);
}
